﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace QuanLyKhachSan
{
    public partial class formtimkiemkhachhang : Form
    {
        DataSet ds_kh;
        OleDbDataAdapter daCus_kh;

        public formtimkiemkhachhang()
        {
            InitializeComponent();
        }

        private void btnthoat_kh_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Quan_LyKhachHang_Load(object sender, EventArgs e)
        {
            ds_kh = new DataSet();
           
            //string strKetNoi_kh = @"Provider=Microsoft.Jet.OleDB.4.0; Data Source=D:\CD TH 11C\Nam III - HK V\Đồ An\QuanLyKhachSan - Copy\CSDLquanlykhachsan.mdb";
            string strKetNoi_kh = @"Provider=Microsoft.Jet.OleDB.4.0; Data Source=D:\CD TH 11C\Nam III - HK V\Công Nghệ Phần Mềm\Đồ Án Lớn\QuanLyKhachSan\CSDLquanlykhachsan.mdb";
            string strSelect_kh = @"SELECT * FROM KhachHang";
            string strInsert_kh = @"INSERT INTO KhachHang(TenKH, NgaySinh, GioiTinh, DienThoai) VALUES (?,?,?,?)";
            string strUpdate_kh = @"UPDATE KhachHang SET TenKH=?, NgaySinh=?, GioiTinh=?, DienThoai=? WHERE MaKH=?";
            string strDelete_kh = @"DELETE FROM KhachHang WHERE  MaNV=?";

            daCus_kh = new OleDbDataAdapter(strSelect_kh, strKetNoi_kh);

            OleDbConnection con = new OleDbConnection(strKetNoi_kh);
           
            // Doi tuong InsertCommand
            OleDbCommand comIns_kh = new OleDbCommand(strInsert_kh, con);
            comIns_kh.Parameters.Add("@hoten", OleDbType.WChar, 30, "TenKH");
            comIns_kh.Parameters.Add("@cmnd", OleDbType.WChar, 20, "CMND");
            comIns_kh.Parameters.Add("@ngaysinh", OleDbType.WChar, 10, "NgaySinh");
            comIns_kh.Parameters.Add("@gioitinh", OleDbType.WChar, 50, "GioiTinh");
            comIns_kh.Parameters.Add("@dienthoai", OleDbType.WChar, 15, "DienThoai");
            daCus_kh.InsertCommand = comIns_kh;

            // Doi tuong DeleteCommand
            OleDbCommand comDel_kh = new OleDbCommand(strDelete_kh, con);
            comDel_kh.Parameters.Add("@makh", OleDbType.Integer, 4, "MaKH");
            daCus_kh.DeleteCommand = comDel_kh;

            // Doi tuong UpdateCommand
            OleDbCommand comUp_kh = new OleDbCommand(strUpdate_kh, con);
            comUp_kh.Parameters.Add("@hoten", OleDbType.WChar, 30, "TenKH");
            comUp_kh.Parameters.Add("@cmnd", OleDbType.WChar, 20, "CMND");
            comUp_kh.Parameters.Add("@ngaysinh", OleDbType.WChar, 10, "NgaySinh");
            comUp_kh.Parameters.Add("@gioitinh", OleDbType.WChar, 50, "GioiTinh");
            comUp_kh.Parameters.Add("@dienthoai", OleDbType.WChar, 15, "DienThoai");
            comUp_kh.Parameters.Add("@makh", OleDbType.Integer, 4, "MaKH");
            daCus_kh.UpdateCommand = comUp_kh;

            // Lay du lieu tu Database do vao Dataset
            daCus_kh.Fill(ds_kh, "tblKhachHang");

            // Dua du lieu tu bang Customer len datagridview
            dataGridView1_kh.DataSource = ds_kh.Tables["tblKhachHang"];            
        }

        private void btnthemmoi_kh_Click(object sender, EventArgs e)
        {
            txthoten_kh.Enabled = true;
            txtngaysinh_kh.Enabled = true;
            txtgioitinh_kh.Enabled = true;
            txtdiachi_kh.Enabled = true;
            txtsdt_kh.Enabled = true;
            txtsocmnd_kh.Enabled = true;
            btnluu_kh.Enabled = true;
            btnthoat_kh.Enabled = true;
            btnsua_kh.Enabled = true;
            btnxoa_kh.Enabled = true;
        }

        private void btnsua_kh_Click(object sender, EventArgs e)
        {
            // Lay dong duoc chon tren Datagridview
            DataGridViewRow dr = dataGridView1_kh.SelectedRows[0];

            // Cap nhat gia tri cho cac truong tren dong duoc chon
            // theo cac textbox tuong ung
            dataGridView1_kh.BeginEdit(true);
            dr.Cells["TenKH"].Value = txthoten_kh.Text;
            dr.Cells["CMND"].Value = txtsocmnd_kh.Text;
            dr.Cells["NgaySinh"].Value = txtngaysinh_kh.Text;
            dr.Cells["GioiTinh"].Value = txtgioitinh_kh.Text;
            dr.Cells["DienThoai"].Value = txtsdt_kh.Text;           
            dataGridView1_kh.EndEdit();
        }

        private void btnxoa_kh_Click(object sender, EventArgs e)
        {
            DataGridViewRow dr = dataGridView1_kh.SelectedRows[0];

            // Remove dong duoc chon khoi datagridview
            dataGridView1_kh.Rows.Remove(dr);
        }

        private void btnluu_kh_Click(object sender, EventArgs e)
        {
            DataRow r = ds_kh.Tables["tblKhachHang"].NewRow();

            //--Nhap gia tri vao cac truong tuong ung           
            r["TenKH"] = txthoten_kh.Text;
            r["CMND"] = txtsocmnd_kh.Text;
            r["NgaySinh"] = txtngaysinh_kh.Text;
            r["GioiTinh"] = txtgioitinh_kh.Text;
            r["DienThoai"] = txtsdt_kh.Text;
          
            //--Chen dong vao bang tblCustomer
            ds_kh.Tables["tblKhachHang"].Rows.Add(r);

            daCus_kh.Update(ds_kh, "tblKhachHang");
            MessageBox.Show("Luu thanh cong");

            //xu ly cac nut 
            btnsua_kh.Enabled = true;
            btnxoa_kh.Enabled = true;

        }




    }
}
